<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit();
}

/**
 * Mileage
**/
class Mileage_ticket extends Legacy_BlockProcedure
{
	/**
	 * @var Mileage_MileHandler
	 * 
	 * @private
	**/
	var $_mHandlerMile = null;

	/**
	 * @var Mileage_RecHandler
	 * 
	 * @private
	**/
	var $_mHandlerRec = null;

	/**
	 * @var Mileage_MileObject
	 * 
	 * @private
	**/
	var $_mObjectMile = null;

	/**
	 * @var string[]
	 * 
	 * @private
	**/
	var $_mOptions = array();

	/**
	 * @var object
	 * 
	 * @private
	**/
	var $_actionForm = NULL;

	/**
	 * prepare
	 * 
	 * @param   void
	 * 
	 * @return  bool
	 * 
	 * @public
	**/
	function prepare()
	{
		return parent::prepare() && $this->_parseOptions() && $this->_setupObject();
	}
	
	/**
	 * _parseOptions
	 * 
	 * @param   void
	 * 
	 * @return  bool
	 * 
	 * @private
	**/
	function _parseOptions()
	{
		$opts = explode('|', $this->_mBlock->get('options'));
		$this->_mOptions = array(
		);

		return true;
	}
	
	/**
	 * getBlockOption
	 * 
	 * @param   string  $key
	 * 
	 * @return  string
	 * 
	 * @public
	**/
	function getBlockOption($key)
	{
		return isset($this->_mOptions[$key]) ? $this->_mOptions[$key] : null;
	}
	
	/**
	 * getOptionForm
	 * 
	 * @param   void
	 * 
	 * @return  string
	 * 
	 * @public
	**/
	function getOptionForm()
	{
		if (!$this->prepare()) {
			return null;
		}
	}

	/**
	 * _setupObject
	 * 
	 * @param   void
	 * 
	 * @return  bool
	 * 
	 * @private
	**/
	function _setupObject()
	{
		$root =& XCube_Root::getSingleton();
		$uid = 0;
		if (is_object($root->mContext->mXoopsUser)) {
			$uid = $root->mContext->mXoopsUser->get('uid');
		} else {
			return FALSE;
		}
		// user group
		$memberHandler =& Mileage_Utils::getXoopsHandler('member');
		$groupsByUid = $memberHandler->getGroupsByUser($uid);

		$dirname = $this->_mBlock->get('dirname');

		//get module asset for handlers
		$asset = null;
		XCube_DelegateUtils::call(
			'Module.mileage.Global.Event.GetAssetManager',
			new XCube_Ref($asset),
			$dirname
		);

		// MILE
		$_mHandlerMile =& $asset->getObject('handler', 'mile');
		$mileCriteria = new CriteriaCompo('1', '1');
		$mileCriteria->add(new Criteria('`url`', 'ticket:'));
		$mileCriteria->add(new Criteria('`url`', 'ticket%u'. intval($uid) .'|%', 'LIKE'), 'OR');
		foreach ($groupsByUid as $group) {
			$mileCriteria->add(new Criteria('`url`', 'ticket%g'. intval($group) .'|%', 'LIKE'), 'OR');
		}
		$objectMile =& $_mHandlerMile->getObjects($mileCriteria);

		if (count($objectMile) == 0) {
			return;
		}

		// REC
		$_mHandlerRec =& $asset->getObject('handler', 'rec');

		for ($i = 0; $i < count($objectMile); $i++) {
			$mile_id = $objectMile[$i]->get('mile_id');
			$interval = $objectMile[$i]->get('interval');
			$objectMile[$i]->canBuy = Mileage_Utils::notExistsMileIDinRec($_mHandlerRec, $mile_id, $interval, $uid, $dirname);
		}

		$this->_mObjectMile = $objectMile;
		$this->_actionForm =& $asset->getObject('form', 'rec', FALSE, 'edit');

		return TRUE;
	}

	/**
	 * execute
	 * 
	 * @param   void
	 * 
	 * @return  void
	 * 
	 * @public
	**/
	function execute()
	{
		$root =& XCube_Root::getSingleton();

		$dirname = $this->_mBlock->get('dirname');

		$render =& $this->getRenderTarget();
		$render->setTemplateName($this->_mBlock->get('template'));

		$render->setAttribute('mile', $this->_mObjectMile);
		$render->setAttribute('dirname', $dirname);
		$render->setAttribute('lang', Mileage_Utils::getConstantLang($dirname));
		$render->setAttribute('isMobile', Mileage_Utils::checkMobile());
		$render->setAttribute('actionForm', $this->_actionForm);
		$render->setAttribute('access_uri', Mileage_Utils::getAccessUri());
		$render->setAttribute('totalPoint', Mileage_Utils::totalMileByUid($dirname));

		$renderSystem =& $root->getRenderSystem($this->getRenderSystemName());
		$renderSystem->renderBlock($render);
	}

}
